/*
    Kelompok    2
    Kelas       2
    Anggota :
    1. Yudha Wastu Prawira      / 13509002
    2. Robertus Theodore        / 13509008
    3. Rendy Bambang Junior     / 13509036
    4. Hartono Sulaiman Wijaya  / 13509046
    5. Daniel Prihartoni        / 13509088
    6. Raydhitya Yosep          / 13509092

*/
#include "Farm.h"

//ctor
Farm::Farm(){
}

//dtor
Farm::~Farm(){
	//jangan lupa dtor semua plant
}

//inisialisasi untuk awal game
void Farm::initialize(){
	//inisialisasi portal dan selling point
	sellingPoint.x = DEFAULT_SELLING_POINT_X;
	sellingPoint.y = DEFAULT_SELLING_POINT_Y;
	portalPoint.x = DEFAULT_PORTAL_X;
	portalPoint.y = DEFAULT_PORTAL_Y;

	//inisialisasi isi kebun
    farmPlan.resize(FARM_HEIGHT);
	for (int i = 0 ; i<= FARM_HEIGHT-1; i++){
        farmPlan[i].resize(FARM_WIDTH);
        for (int j=0; j<=FARM_WIDTH-1; j++){
            farmPlan[i][j].plant = NULL;
			farmPlan[i][j].plantable = true;
			farmPlan[i][j].plowed = false;
			farmPlan[i][j].watered = false;
			farmPlan[i][j].accessible = true;
        }
    }

	for (int i = 0 ; i <= 1 ; i ++){
		for(int j=0; j<=FARM_WIDTH-1;j++){
			farmPlan[i][j].plantable = false;
			farmPlan[i][j].accessible = true;
		}
	}

	farmPlan[sellingPoint.x][sellingPoint.y].accessible = false;

	farmPlan[2][4].plantable=false;
	farmPlan[1][9].plantable=false;
	farmPlan[3][7].plantable=false;
	farmPlan[4][5].plantable=false;
	farmPlan[5][9].plantable=false;
	farmPlan[7][3].plantable=false;
	farmPlan[7][6].plantable=false;
	farmPlan[8][8].plantable=false;

	//buat coba2 ajah
	//farmPlan[5][3].plant = new Plant(CARROT_PLANT_CODE);
	//for(int i=0; i < 4;i++)
	//	farmPlan[5][3].plant->incHappyMeter();
}

//mengubah atribut kebun ketika pemain tidur (ganti hari)
void Farm::nextDay(){
	for (int i = 0 ; i<= FARM_HEIGHT-1; i++){
        for (int j=0; j<=FARM_WIDTH-1; j++){
			if(farmPlan[i][j].plant != NULL){
				farmPlan[i][j].plant->decAgeLeft();
				if (farmPlan[i][j].watered)
					farmPlan[i][j].plant->incHappyMeter();
			}
			farmPlan[i][j].watered = false;
			farmPlan[i][j].plowed = false;
        }
    }
}

void Farm::initialize(SaveInfo saveInfo){
    int i = 0;
    while ((saveInfo.FP.plantable[i].x!=0) && (i<121)){
        Point p = saveInfo.FP.plantable[i];
        farmPlan[p.x][p.y].plantable = true;
        i++;
    }
    i = 0;
    while ((saveInfo.FP.plowed[i].x!=0) && (i<121)){
        Point p = saveInfo.FP.plowed[i];
        farmPlan[p.x][p.y].plowed = true;
        i++;
    }
    i = 0;
    while ((saveInfo.FP.watered[i].x!=0) && (i<121)){
        Point p = saveInfo.FP.watered[i];
        farmPlan[p.x][p.y].watered = true;
        i++;
    }
    i = 0;
    while ((saveInfo.FP.noacc[i].x!= 0) && (i<121)){
        Point p = saveInfo.FP.noacc[i];
        farmPlan[p.x][p.y].accessible = false;
        i++;
    }
    i = 0;
    while ((saveInfo.AP[i].PT!=0) && (i<121)){
        Point p1;
        p1.x = saveInfo.AP[i].Position.x;
        p1.y = saveInfo.AP[i].Position.y;
        setPlanted(p1,saveInfo.AP[i].PT);
        //farmPlan[p1.x][p1.y].plant->initialize(saveInfo.AP[i]);
        i++;
    }
}

//setter
//mengubah koordinat tersebut menjadi tercangkul
void Farm::setPlowed(Point p){
	farmPlan[p.x][p.y].plowed = true;
}

//mengubah koordinat tersebut menjadi tersiram
void Farm::setWatered(Point p){
	farmPlan[p.x][p.y].watered = true;
}

//menanam tanaman dengan tipe plantType di koordinat tersebut
void Farm::setPlanted(Point p, SeedType seedType){
	switch(seedType){
	case CARROT_SEED_CODE :
		farmPlan[p.x][p.y].plant = new Plant(CARROT_PLANT_CODE);
		break;
	case CHILLI_SEED_CODE :
		farmPlan[p.x][p.y].plant = new Plant(CHILLI_PLANT_CODE);
		break;
	case TOMATO_SEED_CODE :
		farmPlan[p.x][p.y].plant = new Plant(TOMATO_PLANT_CODE);
		break;
	case STRAWBERRY_SEED_CODE :
		farmPlan[p.x][p.y].plant = new Plant(STRAWBERRY_PLANT_CODE);
		break;
	case MELON_SEED_CODE :
		farmPlan[p.x][p.y].plant = new Plant(MELON_PLANT_CODE);
		break;
	}
}

//mengubah koordinat tersebut menjadi kosong
void Farm::setEmpty(Point p){
	farmPlan[p.x][p.y].accessible = true;
}

//menyabit tanaman di koordinat tersebut
void Farm::setSlashed(Point p){
	delete farmPlan[p.x][p.y].plant;
	farmPlan[p.x][p.y].plant = NULL;
	farmPlan[p.x][p.y].accessible = true;
	farmPlan[p.x][p.y].plantable = true;
}

//mengatur atribut tanaman dikoordinat tersebut menjadi 'terpanen', lalu return tipe buahnya
void Farm::setHarvested(Point p){
	//jika tanaman bertipe dipanen sekali saja, langsung destruct, bila tidak reset happy meternya
	if(farmPlan[p.x][p.y].plant->getHarvestType()==HARVEST_ONCE){
		delete farmPlan[p.x][p.y].plant;
		farmPlan[p.x][p.y].plant = NULL;
		farmPlan[p.x][p.y].accessible = true;
		farmPlan[p.x][p.y].plantable = true;
	}
	else{
		farmPlan[p.x][p.y].plant->resetHappyMeter();
		farmPlan[p.x][p.y].plant->incHarvestPoint();
	}
}

//mengembalikan atribut dari farm
bool Farm::isEmpty(Point p){
	if (farmPlan[p.x][p.y].plant==NULL)
		return farmPlan[p.x][p.y].accessible;
	else
		return !farmPlan[p.x][p.y].plant->isAdult();
}
bool Farm::isPlowable(Point p){
	return (!farmPlan[p.x][p.y].plowed && farmPlan[p.x][p.y].plantable && farmPlan[p.x][p.y].accessible);
}
bool Farm::isPlantable(Point p){
	return farmPlan[p.x][p.y].plantable;
}
bool Farm::isWaterable(Point p){
	return ((!farmPlan[p.x][p.y].watered && farmPlan[p.x][p.y].plantable && farmPlan[p.x][p.y].accessible && farmPlan[p.x][p.y].plowed)|| farmPlan[p.x][p.y].plant!=NULL);
}
bool Farm::isSlashable(Point p){
	return farmPlan[p.x][p.y].plant!=NULL;
}
bool Farm::isHarvestable(Point p){
	if (farmPlan[p.x][p.y].plant!=NULL)
		return (farmPlan[p.x][p.y].plant->getHappyMeter()>=farmPlan[p.x][p.y].plant->getHarvestPoint());
	else
		return false;
}
bool Farm::isDeadPlant(Point p){
	if (farmPlan[p.x][p.y].plant!=NULL)
		return (farmPlan[p.x][p.y].plant->getAgeLeft()<=0);
	else
		return false;
}

bool Farm::isPlowed(Point p){
	return farmPlan[p.x][p.y].plowed;
}

//mendapatkan farmPlan
FarmPlan Farm::getFarmPlan(){
	return farmPlan;
}

//mendapatkan tipe buah dari tanaman pada koordinat tsb
FruitType Farm::getFruitType(Point p){
	return farmPlan[p.x][p.y].plant->getPlantType();
}

void Farm::getSaveInfo(SaveInfo& saveInfo){
    int a = 0, b = 0, c = 0, d=0, e=0;
    for (int i = 0 ; i<121; i++){
        saveInfo.AP[i].PT = NULL;
        saveInfo.AP[i].HM = NULL;
        saveInfo.AP[i].AL = NULL;
        Point p; p.x = NULL; p.y = NULL;
        saveInfo.AP[i].Position = p;
    }
    for (int i = 0 ; i<= FARM_HEIGHT-1; i++){
        for (int j=0; j<=FARM_WIDTH-1; j++){
            Point p; p.x = i; p.y = j;
			if(farmPlan[i][j].plant != NULL){
				saveInfo.AP[a].PT = farmPlan[i][j].plant->getPlantType();
				saveInfo.AP[a].HM = farmPlan[i][j].plant->getHappyMeter();
				saveInfo.AP[a].AL = farmPlan[i][j].plant->getAgeLeft();
				saveInfo.AP[a].Position = p;
				a++;
			}
			if(farmPlan[i][j].plantable){
                saveInfo.FP.plantable[b] = p;
                b++;
			}
			if(farmPlan[i][j].plowed){
                saveInfo.FP.plowed[c] = p;
                c++;
			}
			if(farmPlan[i][j].watered){
                saveInfo.FP.watered[d] = p;
                d++;
			}
			if(!farmPlan[i][j].accessible){
                saveInfo.FP.noacc[e] = p;
                e++;
			}
        }
    }
}
